﻿/**
 * SLURM FRAMEWORK
 * VERSION: Beta 1.0
 * DATE: 2012-06-13
 * AUTHOR: Ezequiel Soler
 * GNU Lesser GPL
 **/
package com.slurm
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.ByteArray;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;
	import flash.display.DisplayObject;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;

	public class Core extends MovieClip
	{
		[Embed(source='../script.xml',
		        mimeType="application/octet-stream")]
		public static const ScriptScreens:Class;

		private var screensXml:XML;
		private var screenIdStart:Number;
		public var screen:Sprite;
		public var screenXdefault:Number = 0;
		public var screenYdefault:Number = 0;
		
		private var percentajeInit:Number;
		public var percentajeLoader:Number;
		
		private var debugMode:Boolean = false;
		private var slurmMonitor:SlurmMonitor;

		public static var _instance:Object;

		public function Core(screenId:Number = 0)
		{
			screenIdStart = screenId;
			percentajeInit = Math.floor((loaderInfo.bytesLoaded/loaderInfo.bytesTotal)*100);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			addEventListener(Event.ENTER_FRAME, checkLoading);
			trace('Star loading.');
			trace("-----------------");
		}


		private function onProgress(e:ProgressEvent):void
		{
			percentajeLoader = Math.floor((e.bytesLoaded/e.bytesTotal)*(100+percentajeInit))-percentajeInit;
			trace(percentajeLoader+"%");
			updateLoader();
		}
		
		public function updateLoader()
		{
			
		}

		function checkLoading(e:Event):void
		{
			if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal)
			{
				loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
				removeEventListener(Event.ENTER_FRAME, checkLoading);
				_instance = this;
				trace("-----------------");
				trace("Loading complete.");
				trace("-----------------");
				ready();
			}
		}

		public function ready()
		{

		}

		public function render()
		{
			try{
			var fileXml:ByteArray = new ScriptScreens  ;
			var strFile:String = fileXml.readUTFBytes(fileXml.length);
			screensXml = new XML(strFile);
			trace('Slurm status: OK');
			trace("-----------------");
			debugMode = screensXml.@debug == "true" ? true : false;
			if(screensXml.@lan != ""){
			GlobalData.lan = screensXml.@lan;
			}
			if(debugMode){
			trace('Debug mode: ON');
			trace("-----------------");
			slurmMonitor = new SlurmMonitor();
			addChild(slurmMonitor);
			var slurm_cMenu:ContextMenu = new ContextMenu();
			slurm_cMenu.hideBuiltInItems();
			var consoleItem:ContextMenuItem = new ContextMenuItem("Ocultar Slurm Monitor");
			consoleItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, actionMonitor);
			slurm_cMenu.customItems.push(consoleItem);
			contextMenu = slurm_cMenu;
			}else{
			trace('Debug mode: OFF');
			trace("-----------------");
			}
			trace('List of screens:');
			for each (var screenData:XML in screensXml.screen)
			{
				trace("-"+screenData.@name);
			}
			trace("-----------------");
			trace("Screen Width: "+stage.stageWidth);
			trace("Screen Height: "+stage.stageHeight);
			trace("-----------------");
			showScreen(screenIdStart);
			}catch(error:Error){
				trace("ERROR #01: Archivo base Slurm mal formado o defectuoso.");
				trace('Slurm status: FATAL ERROR');
			}
		}

		public static function get instance():Object
		{
			return _instance;
		}

		public function showScreen(idScreen:Number = 0)
		{
			if (screen != null)
			{
				trace(screen+" desactive");
				trace("-----------------");
				removeChild(screen);
				screen = null;
			}
			try{
			var ScreenReference:Class = loaderInfo.applicationDomain.getDefinition("com."+screensXml.screen[idScreen].@name) as Class;
			screen = new ScreenReference(screensXml.screen[idScreen].toString());
			screen.x = screenXdefault;
			screen.y = screenYdefault;
			addChild(screen);
			if(debugMode){
				addChild(slurmMonitor);
			}
			}catch(error:Error){
				trace("ERROR #02: El Screen "+screensXml.screen[idScreen].@name+" no existe.");
				trace('Slurm status: FATAL ERROR');
			}
		}
		
		private function actionMonitor (e:ContextMenuEvent){
			if(slurmMonitor.visible = !slurmMonitor.visible){
			e.currentTarget.caption = "Ocutar Slurm Monitor";
			}else{
			e.currentTarget.caption = "Mostrar Slurm Monitor";
			}
		}

	}

}